package IOStream.IOStreamDome;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * ClassName:IOStream003
 * Package:IOStream.IOStreamDome
 * Description:
 *
 * @Author:ZhangXin
 * @Create2023/2/28-21:11
 * @Version:v1.0
 */
public class IOStream003 {
    /**
     * try...catch...finally 异常处理
     * <p>
     * 特点：finally里面的代码一定被执行,除法JVM虚拟机停止
     * <p>
     * AutoCloseable特点：特定情况下,自动释放资源
     */
    public static void main(String[] args) {
        //创建对象
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("E:\\aaa\\b.txt");
            fos = new FileOutputStream("E:\\aaa\\bbb\\copy1.txt");
            //拷贝
            int len;
            byte[] bytes = new byte[1024 * 1024 * 3];
            while ((len = fis.read()) != -1) {
                fos.write(bytes, 0, len);
            }

        } catch (IOException e) {
//            throw new RuntimeException(e);
//            e.printStackTrace();
        } finally {
            //释放资源
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (fis == null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
